home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / controls / obj1hq.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1993-05-16  |  9.0 KB  |  269 lines

  1. VERSION 2.00
  2. Begin Form frmHQ 
  3.    Caption         =   "Headquarters"
  4.    ClientHeight    =   5385
  5.    ClientLeft      =   2055
  6.    ClientTop       =   1455
  7.    ClientWidth     =   2490
  8.    Height          =   5790
  9.    Left            =   1995
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   5385
  12.    ScaleWidth      =   2490
  13.    Top             =   1110
  14.    Width           =   2610
  15.    Begin CommandButton Command7 
  16.       Caption         =   "End"
  17.       Height          =   495
  18.       Left            =   600
  19.       TabIndex        =   6
  20.       Top             =   4320
  21.       Width           =   1215
  22.    End
  23.    Begin CommandButton Command6 
  24.       Caption         =   "Color All Pictures #2"
  25.       Height          =   495
  26.       Left            =   600
  27.       TabIndex        =   5
  28.       Top             =   3600
  29.       Width           =   1215
  30.    End
  31.    Begin CommandButton Command5 
  32.       Caption         =   "Cross Hatch"
  33.       Height          =   495
  34.       Left            =   600
  35.       TabIndex        =   4
  36.       Top             =   3000
  37.       Width           =   1215
  38.    End
  39.    Begin CommandButton Command4 
  40.       Caption         =   "Color Specific Picture"
  41.       Height          =   495
  42.       Left            =   600
  43.       TabIndex        =   3
  44.       Top             =   2400
  45.       Width           =   1215
  46.    End
  47.    Begin CommandButton Command3 
  48.       Caption         =   "Color All Pictures #1"
  49.       Height          =   495
  50.       Left            =   600
  51.       TabIndex        =   2
  52.       Top             =   1680
  53.       Width           =   1215
  54.    End
  55.    Begin CommandButton Command2 
  56.       Caption         =   "Position Controls"
  57.       Height          =   495
  58.       Left            =   600
  59.       TabIndex        =   1
  60.       Top             =   960
  61.       Width           =   1215
  62.    End
  63.    Begin CommandButton Command1 
  64.       Caption         =   "Load other forms."
  65.       Height          =   495
  66.       Left            =   600
  67.       TabIndex        =   0
  68.       Top             =   240
  69.       Width           =   1215
  70.    End
  71. Option Explicit
  72. Const CMD_COUNT = 7
  73. Const FORMS_COUNT = 3
  74. Const PIC_COUNT = 6
  75. 'Create an array of Form references.
  76. Dim MyForms(FORMS_COUNT) As frmForm1
  77. 'Create an array of Picture references for the picture
  78. 'boxes from the other 3 forms.
  79. Dim MyPictures(PIC_COUNT) As PictureBox
  80. 'Create an array of references to command buttons
  81. Dim MyCommands(CMD_COUNT) As commandbutton
  82. 'The previous declaration could be made more generic like this.
  83. 'Dim MyCommands(CMD_COUNT) As control
  84. Sub Command1_Click ()
  85.     Dim i As Integer
  86.     'Declare a specific form variable
  87.     Dim ThisForm As frmForm1
  88.     'Or, the preceeding variable could have been generic.
  89.     'Specific object variables are more efficient.
  90.     'Dim ThisForm as Form
  91.     'Initialize the array of form1 references.
  92.     'Note that there's an implicit Load statement in a Set.
  93.     'First, refer to the form obtained at design time.
  94.     Set MyForms(1) = frmForm1
  95.     'Create 2 additional copies of form1, and 'point' at them.
  96.     Dim form2 As New frmForm1
  97.     Set MyForms(2) = form2
  98.     Dim form3 As New frmForm1
  99.     Set MyForms(3) = form3
  100.         
  101.     'Position the forms, set their tags, and show them.
  102.     'Use of ThisForm as a 'pointer' simplifies the statements.
  103.     For i = 1 To FORMS_COUNT
  104.         Set ThisForm = MyForms(i)
  105.         ThisForm.Width = screen.Width * .25
  106.         ThisForm.Height = screen.Height
  107.         ThisForm.Top = 0
  108.         ThisForm.Left = i * .25 * screen.Width
  109.         ThisForm.Caption = "Form #" & i
  110.         'Each form's tag becomes a number.
  111.         ThisForm.Tag = Str$(i)
  112.         ThisForm.Show
  113.     Next i
  114.     'Here's another way of doing it. Notice how not using a reference
  115.     'variable makes the individual statements longer.
  116.     'For i = 2 To FORMS_COUNT
  117.     '    MyForms(i).Width = screen.Width * .25
  118.     '    MyForms(i).Height = screen.Height
  119.     '    MyForms(i).Top = 0
  120.     '    MyForms(i).Left = i * .25 * screen.Width
  121.     '    MyForms(i).Caption = "Type #" & i
  122.         'Each form's tag becomes a number.
  123.      '   MyForms(i).Tag = Str$(i)
  124.      '   MyForms(i).Show
  125.      'Next i
  126.     'Disable this control, and enable the next one.
  127.     command1.Enabled = False
  128.     Command2.Enabled = True
  129. End Sub
  130. Sub Command2_Click ()
  131.     Dim i As Integer
  132.     'Initialize the array of picture references. The beauty of this is
  133.     'that it permits treating all controls from all forms in a program
  134.     'with a single array.
  135.     'The complicated expression in the 2 SET statements maps
  136.         '1 to 1 and 2
  137.         '2 to 3 and 4
  138.         '3 to 5 and 6, etc.
  139.     'The ! is the preferred separator for a form and its controls.
  140.     'Thus, use form!control but form.property and form.method
  141.     For i = 1 To FORMS_COUNT
  142.         Set MyPictures(2 * i - 1) = MyForms(i)!picP1
  143.         Set MyPictures(2 * i) = MyForms(i)!picP2
  144.     Next i
  145.         
  146.     'Position the picture controls.
  147.     For i = 1 To FORMS_COUNT
  148.         CenterPicture MyForms(i), MyPictures(2 * i - 1), UPPER
  149.         CenterPicture MyForms(i), MyPictures(2 * i), LOWER
  150.     Next i
  151.     Command2.Enabled = False
  152.     For i = 3 To CMD_COUNT - 1
  153.         MyCommands(i).Enabled = True
  154.     Next i
  155. End Sub
  156. Sub Command3_Click ()
  157.     Dim i As Integer
  158.     For i = 1 To PIC_COUNT
  159.         SetColor MyPictures(i)
  160.     Next i
  161. End Sub
  162. Sub Command4_Click ()
  163.     Dim rc As Integer
  164.     rc = Val(InputBox$("Enter a number between 1 and 6", "Change Color"))
  165.     If (rc < 1 Or rc > 6) Then
  166.         Exit Sub
  167.     End If
  168.     SetColor MyPictures(rc)
  169. End Sub
  170. Sub Command5_Click ()
  171.     Dim i As Integer
  172.     Dim rc As Integer
  173.     Dim x1 As Single, y2 As Single
  174.     'The specific object variable TP will be used as an alias to
  175.     'simplify the code. This is most useful when a control with
  176.     'a lengthy name will be used a lot in a given subroutine.
  177.     Dim TP As PictureBox
  178.     'Note that TP could have been a generic object variable, though
  179.     'this is NOT as efficient.
  180.     'Dim TP As control
  181.     rc = Val(InputBox$("Enter a number between 1 and 6", "Change Color"))
  182.     If (rc < 1 Or rc > 6) Then
  183.         Exit Sub
  184.     End If
  185.     'Point to the requested picture control
  186.     Set TP = MyPictures(rc)
  187.     'Clear the specific picture box by setting its
  188.     'background color to white.
  189.     TP.BackColor = RGB(255, 255, 255)
  190.     'Draw horizontal and vertical lines.
  191.     For i = 1 To 10
  192.         y2 = i * .1 * TP.Height
  193.         TP.Line (0, y2)-(TP.Width, y2)
  194.         
  195.         x1 = i * .1 * TP.Width
  196.         TP.Line (x1, 0)-(x1, TP.Height)
  197.     Next i
  198.     'Notice how ugly and lengthy the statements are when an object
  199.     'variable is NOT used to refer to the requested picture control.
  200.     'For i = 1 To 10
  201.     '    y2 = i * .1 * MyPictures(rc).Height
  202.     '    MyPictures(rc).Line (0, y2)-(MyPictures(rc).Width, y2)
  203.     '
  204.     '    x1 = i * .1 * MyPictures(rc).Width
  205.     '    MyPictures(rc).Line (x1, 0)-(x1, MyPictures(rc).Height)
  206.     'Next i
  207. End Sub
  208. Sub Command6_Click ()
  209.     Dim i As Integer, j As Integer
  210.     'Color all pictures boxes using the forms collection
  211.     'and each form's controls collection.
  212.     For i = 0 To forms.Count - 1
  213.         If TypeOf forms(i) Is frmForm1 Then
  214.             For j = 0 To forms(i).Controls.Count - 1
  215.                 SetColor forms(i).Controls(j)
  216.             Next j
  217.         End If
  218.     Next i
  219. End Sub
  220. Sub Command7_Click ()
  221.     Unload frmHQ
  222. End Sub
  223. Sub Form_Load ()
  224.     Const DELTA1 = 250
  225.     Const DELTA2 = 900
  226.     Const DELTA3 = 250
  227.     Dim i As Integer
  228.     'This form should occupy the left 1/4 of the screen.
  229.     Top = 0
  230.     Left = 0
  231.     Height = screen.Height
  232.     Width = screen.Width * .25
  233.     'The 6 command buttons on this form are placed
  234.     'in an array of controls.
  235.     Set MyCommands(1) = command1
  236.     Set MyCommands(2) = Command2
  237.     Set MyCommands(3) = Command3
  238.     Set MyCommands(4) = Command4
  239.     Set MyCommands(5) = Command5
  240.     Set MyCommands(6) = Command6
  241.     Set MyCommands(7) = Command7
  242.     'Disable buttons 2-6.
  243.     For i = 2 To CMD_COUNT - 1
  244.         MyCommands(i).Enabled = False
  245.     Next i
  246.     'Set every command button's width appropriate to its caption.
  247.     For i = 1 To CMD_COUNT
  248.         MyCommands(i).Width = TextWidth(MyCommands(i).Caption) + DELTA3
  249.     Next i
  250.     MyCommands(1).Top = DELTA1
  251.     MyCommands(1).Left = (ScaleWidth - MyCommands(1).Width) / 2
  252.     'After positioning the first, position the others relative to it.
  253.     For i = 2 To CMD_COUNT
  254.         MyCommands(i).Left = (ScaleWidth - MyCommands(i).Width) / 2
  255.         MyCommands(i).Top = MyCommands(i - 1).Top + DELTA2
  256.     Next i
  257.     'Finally, seed the random number generator.
  258.     Randomize Timer
  259. End Sub
  260. Sub Form_Unload (Cancel As Integer)
  261.     Dim i As Integer
  262.     'Unload the forms in reverse sequence
  263.     If command1.Enabled = False Then
  264.         For i = FORMS_COUNT To 1 Step -1
  265.             Unload MyForms(i)
  266.         Next i
  267.     End If
  268. End Sub
  269.